import os
import django
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import User

 


class ShopWhiteList(models.Model):
    id = models.AutoField('白名单编号', primary_key=True)
    # shop_type = models.CharField('店铺类型', max_length=64, null=False)
    shop_type_choice = (
        ('ebay', 'ebay.com店铺'),
        ('ali', 'aliexpress.com店铺'),
    )
    shop_type = models.CharField('店铺类别',
                                 max_length=20,
                                 choices=shop_type_choice)
    shop_name = models.CharField('店铺名称', max_length=64, null=False)
    class Meta:
        db_table = 'shopwhite_list' 
 

class FollowPrice_lucas(models.Model):
    id = models.AutoField('跟踪编号', primary_key=True)
    shop_type_choice = (
        ('ebay', 'ebay.com店铺'),
        ('ali', 'aliexpress.com店铺'),
    )
    shop_type = models.CharField('店铺类别',
                                 max_length=20,
                                 choices=shop_type_choice)
    storename = models.CharField('店铺', max_length=64, null=False)
    username = models.CharField('负责人', max_length=64, null=False)
    keywords = models.CharField('关键词', max_length=800, null=False)

    title = models.CharField('标题', max_length=800, null=False)
    title2 = models.CharField('标题2', max_length=800, null=True)
    link = models.CharField('链接', max_length=800, null=False)
    pic = models.CharField('图片', max_length=800, null=True)
    price = models.CharField('美元价格', max_length=64, null=False)
    currency = models.CharField('网站显示货币', max_length=64, null=True)
    salesvolume = models.FloatField('销量', max_length=64, null=True)

    salesvolume2 = models.FloatField('销量2', max_length=64, null=True)
    pic2 = models.CharField('图片2', max_length=800, null=True)
    price2 = models.CharField('价格2', max_length=64, null=True)
    itemid = models.CharField(max_length=255, blank=True, null=True)
    productid = models.CharField('平台产品编号',max_length=255, blank=True, null=True)
    lowvalue = models.CharField('跟价最低价格', max_length=64, null=False)
    opdate = models.DateTimeField('操作日期', auto_now=False, auto_now_add=True)
    followstatus = models.CharField('跟价状态',
                                    max_length=64,
                                    default='未跟价',
                                    null=True)
    memo = models.CharField('备注', max_length=800, null=True)
    @property
    def safe_itemid(self):
        return self.itemid or ''
    class Meta:
        db_table = 'follow_price'
        verbose_name = '对方链接跟价-'
        verbose_name_plural = '对方链接跟价列表->新2025（沃尔玛没开通）'
        permissions = [
            ("can_manage_followprice_admin",
             "Can manage followprice in admin"),
        ]
 

class SkuKeywords_lucas(models.Model):
    username = models.CharField('创建人', max_length=64, null=True)

    id = models.AutoField('关键词编号', primary_key=True)
    keywords = models.CharField('关键词', max_length=64, null=False, default='')
    sku = models.CharField('sku编码（不用录入）', max_length=64,blank=True, default='', null=True)

    chargeman = models.CharField('负责人（不用录入）', max_length=64,blank=True, null=True, default='')

    lowvalue = models.FloatField('跟价最低金额（不用录入）', null=True, default=0)
    ebay = models.BooleanField('ebay.com店铺', null=True, default=True)
    ali = models.BooleanField('aliexpress.com店铺', null=True, default=True)
    amz = models.BooleanField('amazon.com店铺', null=True, default=True)
    followstatus = models.CharField('是否跟价过竞品',
                                    max_length=64,
                                    default='未跟价',
                                    null=True)
    captured = models.CharField('抓取状态',default='未抓取', max_length=64, null=True)
    opdate = models.DateTimeField('更新时间', auto_now=True)
    download_date    = models.DateTimeField('下载日期',default=None, blank=True, null=True)
    def save(self, *args, **kwargs):
        if not self.username and hasattr(self, '_current_user'):
            self.username = self._current_user.username
        super().save(*args, **kwargs)
    class Meta:
        db_table = 'sku_keywords'
        verbose_name = "关键词sku名单"
        verbose_name_plural = "输入关键词，可以下载各个平台产品"

class FollowPriceXue(models.Model):
    id = models.AutoField('跟踪编号', primary_key=True)
     
    storename = models.CharField('店铺', max_length=64, null=False, default='Amazon')
    username = models.CharField('负责人', max_length=64, null=False)
    keywords = models.CharField('关键词', max_length=800, null=False)

    title = models.CharField('标题', max_length=800, null=False)
    title2 = models.CharField('标题2', max_length=800, null=True, blank=True)
    link = models.CharField('链接', max_length=2000, null=False)  # 增加长度以适应亚马逊长链接
    pic = models.CharField('图片', max_length=800, null=True, blank=True)
    price = models.CharField('美元价格', max_length=64, null=False)
    currency = models.CharField('网站显示货币', max_length=64, null=True, default='USD')
    salesvolume = models.FloatField('销量', null=True, blank=True)  # 亚马逊可能没有具体销量

    salesvolume2 = models.FloatField('销量2', null=True, blank=True)
    pic2 = models.CharField('图片2', max_length=800, null=True, blank=True)
    price2 = models.CharField('价格2', max_length=64, null=True, blank=True)
    itemid = models.CharField('ASIN', max_length=255, blank=True, null=True)  # 重命名为ASIN
    productid = models.CharField('平台产品编号',max_length=255, blank=True, null=True)
    lowvalue = models.CharField('跟价最低价格', max_length=64, null=False)
    opdate = models.DateTimeField('操作日期', auto_now=False, auto_now_add=True)
    followstatus = models.CharField('跟价状态',
                                    max_length=64,
                                    default='未跟价',
                                    null=True)
    memo = models.CharField('备注', max_length=800, null=True, blank=True)
    
    # 新增字段
    rating = models.FloatField('评分', null=True, blank=True)  # 商品评分
    review_count = models.IntegerField('评论数', null=True, blank=True)  # 评论数量
    prime = models.BooleanField('Prime商品', default=False)  # 是否为Prime商品
    category = models.CharField('商品分类', max_length=200, null=True, blank=True)  # 商品分类
    seller = models.CharField('卖家', max_length=200, null=True, blank=True)  # 卖家信息
    delivery_info = models.TextField('配送信息', null=True, blank=True)  # 配送信息
    
    @property
    def safe_itemid(self):
        return self.itemid or ''

    class Meta:
        db_table = 'app_product_xue'
        verbose_name = '对方链接跟价-'
        verbose_name_plural = '对方链接跟价列表->亚马逊2025'
        permissions = [
            ("can_manage_followprice_admin",
             "Can manage followprice in admin"),
        ]

class SkuKeywordsXue(models.Model):
    username = models.CharField('创建人', max_length=64, null=True)

    id = models.AutoField('关键词编号', primary_key=True)
    keywords = models.CharField('关键词', max_length=64, null=False, default='')
    sku = models.CharField('sku编码（不用录入）', max_length=64,blank=True, default='', null=True)
 
    opdate = models.DateTimeField('更新时间', auto_now=True)
    download_date    = models.DateTimeField('下载日期',default=None, blank=True, null=True)
    def save(self, *args, **kwargs):
        if not self.username and hasattr(self, '_current_user'):
            self.username = self._current_user.username
        super().save(*args, **kwargs)
    class Meta:
        db_table = 'app_keywords_xue'
        verbose_name = "关键词sku名单"
        verbose_name_plural = "输入关键词，可以下载各个平台产品"




class Rates(models.Model):
    id = models.AutoField('汇率编号', primary_key=True)
    currency_name = models.CharField('货币名称', max_length=64, null=True)
    currency_simplename = models.CharField('货币简称-需要和网站一致',
                                           max_length=64,
                                           null=True)
    rate = models.FloatField('汇率', max_length=64, null=True)
    opdate = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'rates'
        verbose_name = "货币汇率列"

        verbose_name_plural = '当前主流货币汇率列表'


class CrawlExcept(models.Model):
    id = models.AutoField('异常编号', primary_key=True)
    shop_type_choice = (
        ('ebay', 'ebay.com店铺'),
        ('ali', 'aliexpress.com店铺'),
    )
    opdate = models.DateTimeField('操作日期', auto_now=False, auto_now_add=False, null=True)

    shop_type = models.CharField('关键词类别',
                                 max_length=20,
                                 choices=shop_type_choice)
    keywords = models.CharField('关键词', max_length=64, null=True)
    exceptcon = models.CharField('异常信息 ', max_length=2000, null=True)

    class Meta:
        db_table = 'crawlexcept'
        verbose_name = "下载异常"

